<?php
namespace app\common;

use dingtalk\Dept;
use think\Model;

class DepartmentModel extends Model
{
    protected $name = 'department'; //定义用户表
    //开启自动写入时间戳
    protected $autoWriteTimestamp = false;
    // 定义自动完成的属性
    protected $auto = []; //新增修改
    protected $insert = []; //插入
    protected $update = []; //修改

    /**
     * 同步钉钉部门
     */
    public function syncDept($accessToken){
        $list = Dept::listDept($accessToken); //所有下级部门
        if($list['errcode'] == 0){
            $deptIds = $list['department'];
            $deptIds[]=['id'=>1];
            foreach($deptIds as $k=>$v){
                $deptInfo = Dept::getDept($accessToken,$v['id']);
                $deptList[$k]['id'] = $deptInfo['id'];
                $deptList[$k]['name'] = $deptInfo['name'];
                $deptList[$k]['parentid'] = $deptInfo['parentid']>0?$deptInfo['parentid']:0;
                $deptList[$k]['dept_userids'] = $deptInfo['deptManagerUseridList'];
            }
            $this->where('1=1')->delete();
            $this->allowField(true)->saveAll($deptList,false);
            return true;
        }
        $this->error = $list['errmsg'];
        return false;
    }
}